{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0f45d67d",
   "metadata": {},
   "source": [
    "## OCR（光学字符识别）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "80a0e99f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.5e8e31ad25d598d5cca95503f9c6066e.315360000.1965126910.282335-25954474', 'expires_in': 2592000, 'session_key': '9mzdWr13sqrZzElwKsmT+yZSrjPwo1XGZVVGdH1Io6JCK+vtpMR/9cKUe1vaGcxhnU+MJ+1uIUKcOVCaj7Z8Yer1MLXICQ==', 'access_token': '24.aab857e2f7e14363e7ea6f5b0a806bb5.2592000.1652358910.282335-25954474', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '26ee583d4c116675ca1c2a363d95dfea'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'hwHlnevrrIejnMyz8CzmjsHg',\n",
    "    'client_secret':'gddq8YGuqYFzc7wfN3C2HfnWR0EK1uAz'\n",
    "}\n",
    "\n",
    "response = requests.get(host, params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ebc188bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.aab857e2f7e14363e7ea6f5b0a806bb5.2592000.1652358910.282335-25954474'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token = response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90c213dc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "20aef9ad",
   "metadata": {},
   "source": [
    "### 通用文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "de247a42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'probability': {'average': 0.9999688268,\n",
       "    'min': 0.9999303818,\n",
       "    'variance': 8.295290566e-10},\n",
       "   'words': '版面上'},\n",
       "  {'probability': {'average': 0.999997437,\n",
       "    'min': 0.9999960661,\n",
       "    'variance': 1.06462983e-12},\n",
       "   'words': '使用的'},\n",
       "  {'probability': {'average': 0.9953913093,\n",
       "    'min': 0.9862448573,\n",
       "    'variance': 4.182843259e-05},\n",
       "   'words': '细文字'},\n",
       "  {'probability': {'average': 0.9999551773,\n",
       "    'min': 0.9998772144,\n",
       "    'variance': 2.181790748e-09},\n",
       "   'words': '画面更加简约精致'},\n",
       "  {'probability': {'average': 0.9974482059,\n",
       "    'min': 0.9903485775,\n",
       "    'variance': 1.006859657e-05},\n",
       "   'words': '知乎@国王与乞丐'}],\n",
       " 'words_result_num': 5,\n",
       " 'log_id': 1513859062826184978}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('通用图片文字.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# params = {\"image\":img}\n",
    "# access_token = '[调用鉴权接口获取的token]'\n",
    "# request_url = request_url + \"?access_token=\" + access_token\n",
    "\n",
    "payload = {\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'probability':'true'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, params=payload, headers=headers)\n",
    "if response:\n",
    "    display (response.json())\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "658636f5",
   "metadata": {},
   "source": [
    "### 网络图片文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ef0c7a0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'language': -1,\n",
       " 'words_result': [{'words': '同'}],\n",
       " 'words_result_num': 1,\n",
       " 'log_id': 1513859582506292937}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('同桌的你.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# params = {\"image\":img}\n",
    "# access_token = '[调用鉴权接口获取的token]'\n",
    "# request_url = request_url + \"?access_token=\" + access_token\n",
    "\n",
    "payload = {\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'detect_language':'true'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, params=payload, headers=headers)\n",
    "if response:\n",
    "    display (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51ed35d5",
   "metadata": {},
   "source": [
    "### 手写文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "52837f10",
   "metadata": {},
   "outputs": [],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('shouxiezi.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# params = {\"image\":img}\n",
    "# access_token = '[调用鉴权接口获取的token]'\n",
    "# request_url = request_url + \"?access_token=\" + access_token\n",
    "\n",
    "payload = {\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'recognize_granularity':'small',\n",
    "\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, params=payload, headers=headers)\n",
    "if response:\n",
    "    display (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c0fdedb",
   "metadata": {},
   "source": [
    "### 二维码识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f4375ab9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'codes_result': [{'text': ['这是一个二维码'], 'type': 'QR_CODE'}],\n",
       " 'codes_result_num': 1,\n",
       " 'log_id': 1513860578601757847}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('erweima.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# params = {\"image\":img}\n",
    "# access_token = '[调用鉴权接口获取的token]'\n",
    "# request_url = request_url + \"?access_token=\" + access_token\n",
    "\n",
    "payload = {\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, params=payload, headers=headers)\n",
    "if response:\n",
    "    display (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e2e7742",
   "metadata": {},
   "source": [
    "### 卡片类文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e4888de8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': {},\n",
       " 'words_result_num': 0,\n",
       " 'image_status': 'non_idcard',\n",
       " 'risk_type': 'unknown',\n",
       " 'idcard_number_type': -1,\n",
       " 'log_id': 1513861882138321368}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('erweima.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# params = {\"image\":img}\n",
    "# access_token = '[调用鉴权接口获取的token]'\n",
    "# request_url = request_url + \"?access_token=\" + access_token\n",
    "\n",
    "payload = {\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'id_card_side':'front',\n",
    "    'detect_risk':'true'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, params=payload, headers=headers)\n",
    "if response:\n",
    "    display (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1592c1a",
   "metadata": {},
   "source": [
    "### 财务类文字识别-增值税发票识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c15a566f",
   "metadata": {},
   "outputs": [],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('fapiao.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# params = {\"image\":img}\n",
    "# access_token = '[调用鉴权接口获取的token]'\n",
    "# request_url = request_url + \"?access_token=\" + access_token\n",
    "\n",
    "payload = {\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, params=payload, headers=headers)\n",
    "if response:\n",
    "    display (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14d6cc8a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
